/**
 * Created by Luji on 2017/03/27.
 */
import editPWTemp from './form/editPWForm.html';
import editPWCtrl from './form/editPWForm.ctrl.js';
import creatTemp from './form/creatForm.html';
import creatCtrl from './form/creatForm.ctrl.js';
import delTmp from './form/delete.html';
import delCtrl from './form/delete.ctrl.js';

class App {
    constructor(list, userSrv, ctgNotification, $uiModal) {
        "ngInject";
        this._userSrv = userSrv;
        this._notification = ctgNotification;
        this._uiModal = $uiModal;

        this.page = {
            currentPage: 1,
            totalItems: 0
        };
        this.filter = {
            name: null
        };

        if (list) {
            this.table = list.list;
            this.page.totalItems = list.total;
        } else this.table = [];
    }

    search() {
        this.page.currentPage = 1;
        this.getList();
    }

    getList() {
        this.table = null;
        this.getData();
    }

    getData(){
        this._userSrv.getUserList({currentPage: this.page.currentPage}, this.filter).then(data=> {
            this.table = data.list;
            this.page.totalItems = data.total;
        }).catch(()=> {
            if(!this.table){
                this.table = [];
                this.page.totalItems = 0;
            }
            this._notification.error('获取用户列表数据有误.');
        });
    }

    deleteUser(elem) {
        this._uiModal.open({
            templateUrl: delTmp,
            controller: delCtrl,
            controllerAs: 'vm',
            windowClass: 'alert-dialog alert-dialog-ctg',
            resolve: {
                alert: function () {
                    return {
                        userName: elem.userName,
                        action: '删除'
                    }
                }
            }
        }).result.then(()=> {
                elem.deleting = true;
                this._userSrv.deleteUser(elem.userId).then(data=> {
                    if (this.table.length == 1 && this.page.currentPage > 1)  this.page.currentPage--;

                    this.getData();
                    this._notification.success('删除用户 ' + elem.userName + ' 成功.');
                }).catch(data=> {
                    this._notification.error('删除用户 ' + elem.userName + ' 失败.');
                }).finally(()=> {
                    elem.deleting = false;
                })
            });
    }

    editPassWord(id){
        this._uiModal.open({
            templateUrl: editPWTemp,
            controller: editPWCtrl,
            controllerAs: 'vm',
            size:'md',
            resolve: {
                id : () => {
                    return id
                }
            }
        }).result.then((data)=> {
        });
    }

    creat(edit){
        this._uiModal.open({
            templateUrl: creatTemp,
            controller: creatCtrl,
            controllerAs: 'vm',
            size:'md',
            resolve: {
                edit : () => {
                    return edit
                }
            }
        }).result.then((data)=> {
            this.getList();
        });
    }
}
export default App